package com.rs.game.player.content.construction;

import com.rs.game.player.Player;

public class RoomConstruction {

	private Player p;
	
	public RoomConstruction(Player player) {
		this.p = player;
	}
	
	private boolean roomExists(Player p, int x, int y) {
		for (RoomReference room : p.getRooms()) {
			if (isBuildingLeft()) {
				if (room.getX() == x - 1 && room.getY() == y && room.getPlane() == p.getPlane()) {
					return true;
				}
			}
			if (isBuildingRight()) {
				if (room.getX() == x + 1 && room.getY() == y && room.getPlane() == p.getPlane()) {
					return true;
				}
			}
			if (isBuildingUp()) {
				if (room.getX() == x && room.getY() == y + 1 && room.getPlane() == p.getPlane()) {
					return true;
				}
			}
			if (isBuildingDown()) {
				if (room.getX() == x && room.getY() == y - 1 && room.getPlane() == p.getPlane()) {
					return true;
				}
			}
		}
		return false;
	}
	
	public void addRoom(Player player, int componentId) {
		if (componentId < 93 || componentId > 109) {
			player.getPackets().sendGameMessage("This room is not yet supported");
		}
		if (roomExists(player, player.getRoomX(), player.getRoomY())) {
			player.getPackets().sendGameMessage("Room deletion is not yet supported, you can not remove rooms.");
			return;
		} else {
			Room r = this.getRoomForComponentId(componentId);
			if (hasRequirements(r) && hasRoomsLeft(player)) {
				buildRoom(r, componentId);
				removeCash(r.getCost());
			} else if (!hasRequirements(r)){
				player.getPackets().sendGameMessage("Make sure you have the needed money and levels before building a room!");
				return;
			} else {
				player.getPackets().sendGameMessage("You aren't able to build any more rooms. Try leveling up to increase the limit!");
				return;
			}
		}
	}
	
	public int getRoomRotationToBuild() {
		switch (p.getLocalX()) {
		case 55:
			return 1;
		case 48:
			return 3;
		}
		switch (p.getLocalY()) {
		case 55:
			return 0;
		case 48:
			return 2;
		}
		return 0;
	}
	
	public boolean doesRoomExist(int roomx, int roomy) {
		for (RoomReference room : p.getRooms()) {
			if (room.getX() == roomx && room.getY() == roomy) {
				return true;
			}
		}
		return false;
	}
	
	public boolean isBuildingLeft() {
		if (p.getLocalX() == 48) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean isBuildingRight() {
		if (p.getLocalX() == 55) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean isBuildingUp() {
		if (p.getLocalY() == 55) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean isBuildingDown() {
		if (p.getLocalY() == 48) {
			return true;
		} else {
			return false;
		}
	}
	
	private Room getRoomForComponentId(int i) {
		return Room.forId(i);
	}
	
	private void removeCash(int cashToRemove) {
		p.getInventory().deleteItem(995, cashToRemove);
	}
	
	private void buildRoom(Room r, int componentId) {
		if (isBuildingLeft()) {
			p.getRooms().add(new RoomReference(r, p.getRoomX() - 1, p.getRoomY(), p.getPlane(), getRoomRotationToBuild()));
		    p.getHouse().constructHouse(p.getBoundChuncks(), true);
		    p.getHouse().refresh(p, p.getBoundChuncks());
		} else if (isBuildingRight()) {
			p.getRooms().add(new RoomReference(r, p.getRoomX() + 1, p.getRoomY(), p.getPlane(), getRoomRotationToBuild()));
		    p.getHouse().constructHouse(p.getBoundChuncks(), true);
		    p.getHouse().refresh(p, p.getBoundChuncks());
		} else if (isBuildingUp()) {
			p.getRooms().add(new RoomReference(r, p.getRoomX(), p.getRoomY() + 1, p.getPlane(), getRoomRotationToBuild()));
		    p.getHouse().constructHouse(p.getBoundChuncks(), true);
		    p.getHouse().refresh(p, p.getBoundChuncks());
		} else if (isBuildingDown()) {
			p.getRooms().add(new RoomReference(r, p.getRoomX(), p.getRoomY() - 1, p.getPlane(), getRoomRotationToBuild()));
		    p.getHouse().constructHouse(p.getBoundChuncks(), true);
		    p.getHouse().refresh(p, p.getBoundChuncks());
		}
	}
	
	private boolean hasRequirements(Room r) {
		return (p.getInventory().getItems().getNumberOf(995) >= r.getCost() && p.getSkills().getLevel(22) >= r.getLevel());
	}
	
	private boolean hasRoomsLeft(Player p) {
		int i = p.getSkills().getLevel(22);
		if (i < 30) {
			return p.getRooms().size() < 20;
		} else if (i < 50) {
			return p.getRooms().size() < 24;
		} else if (i < 70) {
			return p.getRooms().size() < 28;
		} else if (i < 90) {
			return p.getRooms().size() < 32;
		} else if (i == 99) {
			return p.getRooms().size() < 40;
		}
		return false;
	}
}
